package com.qianli.cashmama.trade.domain.services.impl;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.qianli.cashmama.trade.client.bill.request.BillPayRequest;
import com.qianli.cashmama.trade.client.vo.DiscountListVO;
import com.qianli.cashmama.trade.common.enums.TradeDiscountStatusEnum;
import com.qianli.cashmama.trade.common.enums.TradeDiscountTypeEnum;
import com.qianli.cashmama.trade.dao.TDiscountDao;
import com.qianli.cashmama.trade.domain.model.BillBase;
import com.qianli.cashmama.trade.domain.model.TradeBase;
import com.qianli.cashmama.trade.domain.services.TradeDiscountService;
import com.qianli.cashmama.trade.entity.TDiscountEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class TradeDiscountServiceImpl implements TradeDiscountService {

    @Autowired
    TDiscountDao discountDao;

    /**
     * 优惠实扣
     *
     * @param tradeBase
     */
    @Override
    public void deductDiscount(TradeBase tradeBase) {
        if (CollectionUtils.isEmpty(tradeBase.getTradeDiscountList())) {
            return;
        }
        return;
    }

    /**
     * 核销券
     *
     * @param billBase
     * @param billPayRequest
     */
    @Override
    public void verifyDiscount(BillBase billBase, BillPayRequest billPayRequest) {
        BigDecimal discountCapital = BigDecimal.ZERO;
        String discountCode = "";
        List<DiscountListVO> discountList = billPayRequest.getDiscountList();

        if (!CollectionUtils.isEmpty(discountList)) {//计算抵扣金额
            String discountCapital1 = discountList.get(0).getDiscountCapital();
            if (StringUtils.isEmpty(discountCapital1)) {
                return;
            }
            discountCapital = discountCapital.add(new BigDecimal(discountCapital1));
            discountCode = discountList.get(0).getDiscountCode();
        }

        //记录优惠
        TDiscountEntity discountEntity = discountDao.selectOneByTradeNo(billBase.getTradeNo());
        if (null == discountEntity) {
            discountEntity = new TDiscountEntity();
            discountEntity.setDiscountCode(discountCode)
                    .setStatus(com.qianli.cashmama.trade.common.enums.TradeDiscountStatusEnum.VERIFIED.getType())
                    .setTradeNo(billBase.getTradeNo())
                    .setType(!CollectionUtils.isEmpty(discountList) ? discountList.get(0).getDiscountTypeEnum().getType() : TradeDiscountTypeEnum.REDUCTION.getType())
                    .setDetail(billBase.getBillNo())
                    .setCapital(discountCapital)
                    .setOperator(billPayRequest.getOperator());
            discountDao.insert(discountEntity);
        } else {//核销
            discountEntity.setStatus(TradeDiscountStatusEnum.VERIFIED.getType());
            discountDao.updateByTradeNoAndDiscountCode(discountEntity);
        }

        if (StringUtils.isNotEmpty(discountCode)) { //优惠核销

        }
    }

    /**
     * 回滚优惠券
     *
     * @param tradebase
     */
    @Override
    public void rollBackDiscount(TradeBase tradebase) {
        if (CollectionUtils.isEmpty(tradebase.getTradeDiscountList())) {
            return;
        }
        return;
    }
}
